package cn.tiga.pet.controller;

import cn.tiga.pet.service.IPetTypeService;
import cn.tiga.pet.domain.PetType;
import cn.tiga.pet.query.PetTypeQuery;
import cn.tiga.basic.util.JsonResult;
import cn.tiga.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/petType")
public class PetTypeController {
    @Autowired
    public IPetTypeService petTypeService;


    /**
     * 保存和修改公用的
     * @param petType  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public JsonResult addOrUpdate(@RequestBody PetType petType){
        try {
            if( petType.getId()!=null)
                petTypeService.update(petType);
            else
                petTypeService.add(petType);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            petTypeService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return JsonResult.me().setMsg("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public PetType get(@PathVariable("id")Long id)
    {
        return petTypeService.loadById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<PetType> list(){

        return petTypeService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<PetType> json(@RequestBody PetTypeQuery query)
    {
        return petTypeService.pageQuery(query);
    }


    /**
     * @Title: deptTree 获取级联数据
     * @Description: TODO
     * @Author: Pan
     * @Version: 1.0
     * @Date: 2022/7/20 10:54
     * @Parameters: []
     * @Return java.util.List<cn.pping.org.domain.Department>
     */

    @GetMapping("/typeTree")
    public List<PetType> deptTree() {
        return petTypeService.typeTree();
    }
}
